import os
import time

import pytest
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By
from dama import Dama

@pytest.fixture(scope='session')
def getcookie():
    d=getcode()
    header={
        'content-type':'application/x-www-form-urlencoded; charset=UTF-8',
        'cookie':f'JSESSIONID={d.get("JSESSIONID")};JEECGINDEXSTYLE={d.get("JEECGINDEXSTYLE")};ZINDEXNUMBER={d.get("ZINDEXNUMBER")};'
    }
    return header


@pytest.fixture(scope='function')
def getdriver():
    driver=None
    if 'chrome' == os.getenv('drivertypt'):
        driver=webdriver.Chrome()
    if driver is None:
        print('没有匹配的浏览器')
        return
    driver.maximize_window()
    driver.implicitly_wait(5)
    yield driver
    driver.quit()

def getcode():
    driver=webdriver.Chrome()
    driver.save_screenshot(r'D:\gaoliu-2211A\work7\img.png')
    ele=driver.find_element(By.XPATH,'//*[@id="randCodeImage"]')
    size=ele.size
    lcoa=ele.location
    x=509
    y=252
    print(x,y,size,lcoa)
    format=(x,y,x+size['width'],y+size['height'])
    img=Image.open(r'D:\gaoliu-2211A\work7\img.png')
    crop=img.crop(format)
    crop.save(r'D:\gaoliu-2211A\work7\img2.png')
    dm=Dama('weimingxue','123456','970647')
    with open('img2.png','rb')as f:
        yzm=f.read()
    jieguo=dm.PostPic(yzm,1902)
    reuslt=jieguo['pic_str']
    print(reuslt)

    time.sleep(3)
    driver.find_element(By.XPATH,'//*[@id="userName"]').clear()
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[@id="userName"]').send_keys('admin')
    time.sleep(3)
    driver.find_element(By.XPATH,'//*[@id="password"]').clear()
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[@id="password"]').send_keys('llg123')
    time.sleep(3)
    driver.find_element(By.XPATH,'//*[@id="randCode"]').send_keys(reuslt)
    time.sleep(3)
    driver.find_element(By.XPATH,'//*[@id="but_login"]').click()
    time.sleep(3)

    dic={}
    for d in driver.get_cookies():
        for key,value in d.items():
            if value == 'JSESSIONID':
                dic['JSESSIONID']=d.get('value')
            if value == 'JEECGINDEXSTYLE':
                dic['JEECGINDEXSTYLE']=d.get('value')
            if value == 'ZINDEXNUMBER':
                dic['ZINDEXNUMBER']=d.get('value')
    driver.quit()
    return dic




